﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace _58login
{
    public class BoundaryPostData
    {
        public BoundaryPostData(CookieCollection ck)
        {

            this.cklist = ck;
        }

        public string LastStatus;
        public CookieCollection cklist = new CookieCollection();

        public string TicksStr = DateTime.Now.Ticks.ToString("x");
        //  public string TicksStr = "Vms98WyQk7DpItTe";
        public string UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Win64; x64; Trident/7.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0E; .NET4.0C; Tablet PC 2.0; InfoPath.3)";
        public string Accept = "*/*";
        public virtual string Referer
        {
            get;
            set;

        }
        public WebProxy wp { get; set; }
        public virtual string DoRequestPost(bool allowdir, string urla, string postdata)
        {
            return "undefined";
        }
        public string ExeJavascript(string FilePathSrc, string o)
        {
            string c = File.ReadAllText(FilePathSrc);
            //ScriptControlClass js = new ScriptControlClass();
            //js.Language = "javascript";
            //var a = js.Eval("var w=10;var t=2; p=w/t;");  


            //  string o="aaa('qqq')";
            return ExecuteScript(o, c);
        }
        public string ExecuteScript(string sExpression, string sCode)
        {
            MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl();
            scriptControl.UseSafeSubset = false;
            scriptControl.AllowUI = true;

            scriptControl.Language = "JScript";

            scriptControl.AddCode(sCode);
            try
            {
                string str = scriptControl.Eval(sExpression).ToString();
                return str;
            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
            return null;
        }
        private static CookieCollection strCokAddColnew(string s, string defaultDomain, CookieCollection cc)
        {
            try
            {
                s.TrimEnd(new char[] { ';' }).Trim();
                System.Collections.Hashtable hs = new System.Collections.Hashtable();





                foreach (string sa in s.Split(';'))
                {




                    Cookie c = new Cookie();
                    c.Name = Regex.Replace(sa.Trim().Substring(0, sa.Trim().IndexOf('=')), @"\r\n", "").ToString();
                    c.Value = Regex.Replace(sa.Trim().Substring(sa.Trim().IndexOf('=') + 1), @"\r\n", "").ToString();

                    c.Domain = defaultDomain;
                    cc.Add(c);
                }
            }
            catch (Exception e)
            {
                int a = 0;
            }
            return cc;
        }

        public string listallcookies(CookieCollection ckl)
        {

            string s = "";
            foreach (Cookie c in ckl)
            {
                s += c.Name + "=" + System.Web.HttpUtility.UrlDecode(c.Value) + ",";
            }
            return s;
        }
        public string getBysrcAndpattern(string msg, string pt)
        {


            var match2 = Regex.Match(msg, pt);
            string rich2 = match2.Groups[1].Value;
            return rich2;
        }
        public void InitCookie()
        {


            Cookie ck = new Cookie();
            ck.Name = "citydomain";
            ck.Value = "nb";
            ck.Path = "/";
            ck.Domain = ".ganji.com";
            this.cklist.Add(ck);
            ck = new Cookie();
            ck.Name = "lg";
            ck.Value = "1";
            ck.Path = "/";
            ck.Domain = ".ganji.com";
            this.cklist.Add(ck);
        }
        public string doreqoutget(bool allowdir, string urla, string postdata)
        {

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urla);

            req.CookieContainer = new CookieContainer();


            req.CookieContainer.Add(cklist);
            //WebProxy wp = new WebProxy("121.40.195.200:808");

            //req.Proxy = wp;
            req.Method = "GET";

            req.AllowAutoRedirect = allowdir;

            if (!string.IsNullOrEmpty(postdata))
            {

                byte[] bs = Encoding.ASCII.GetBytes(postdata);
                string responseData = String.Empty;

                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = bs.Length;
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);
                    reqStream.Close();
                }
            }
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            //    WebHeaderCollection responseHeaders = res.Headers;

            //if (keepContext)
            //{
            //    context.Cookies = res.Cookies;
            //    context.Referer = _requestUrl;
            //}
            using (Stream stream = res.GetResponseStream())
            {
                using (StreamReader streamReader = new StreamReader(stream))
                {
                    string msg = streamReader.ReadToEnd();

                    int a = res.Cookies.Count;
                    if (a > 0)
                    {


                        cklist.Add(res.Cookies);
                    }
                    //    string lc = res.Headers["Location"].ToString();
                    return msg;
                }
            }

            return "";

        }
    }
}
